package com.gomore.opple.module.order;

import com.gomore.opple.common.GlobalConstant;
import com.gomore.opple.data.DataRepository;
import com.gomore.opple.data.remote.retrofit.HttpResponseFunc;
import com.gomore.opple.data.remote.retrofit.RxSubscriber;
import com.gomore.opple.data.remote.retrofit.ServerResponseFunc;
import com.gomore.opple.exception.ApiException;
import com.gomore.opple.rest.common.DataPage;
import com.gomore.opple.rest.lottery.RsLotteryRequest;
import com.gomore.opple.rest.lottery.RsLotteryResponse;
import com.gomore.opple.rest.order.OrderBill;
import com.gomore.opple.rest.order.OrderFilter;
import com.gomore.opple.rest.order.OrderResponse;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;
import com.gomore.opple.web.cgform.order.entity.TOOrderEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class OrderPresenter implements OrderContract.Presenter {

    private DataRepository mDataRepositroy;
    private final OrderContract.View mView;
    private CompositeSubscription mSubscriptions;
    private List<OrderBill> orderBillList = new ArrayList<>();
    private int rows = GlobalConstant.PAGE_SIZE;
    private int page = GlobalConstant.PAGE_START;

    @Inject
    OrderPresenter(DataRepository dataRepository, OrderContract.View view) {
        mDataRepositroy = dataRepository;
        mView = view;
        mSubscriptions = new CompositeSubscription();
        mView.setPresenter(this);
    }


    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mSubscriptions.clear();
    }

    /**
     * 初始化数据
     */
    @Override
    public void prepareInitData() {

    }

    @Override
    public void queryOrder(final boolean isLoadMore, String state) {
        if (isLoadMore) {
            page++;
        } else {
            orderBillList.clear();
            page = GlobalConstant.PAGE_START;
        }
        mView.showProgressDialog();
        OrderFilter orderFilter = new OrderFilter();
        orderFilter.setOrderStateEquals(state);
        String resellerCode = mDataRepositroy.getUser().getEmployee().getCurrentDepart().getOrgCode();
        orderFilter.setResellerCodeEquals(resellerCode);
        DataPage dataPage = new DataPage();
        dataPage.setRows(rows);
        dataPage.setPage(page);
        orderFilter.setDp(dataPage);
        mSubscriptions.clear();
        Subscription subscription = mDataRepositroy.queryOrder(orderFilter)
                .map(new ServerResponseFunc<OrderResponse>())
                .onErrorResumeNext(new HttpResponseFunc<OrderResponse>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<OrderResponse>() {
                    @Override
                    public void onNext(OrderResponse orderResponse) {
                        super.onNext(orderResponse);
                        mView.hideProgressDialog();
                        if (isLoadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                        if (page <= orderResponse.getTotalpages()) {
                            orderBillList.addAll(orderResponse.getOrderBillList());
                        }
                        mView.showContentView();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                        if (isLoadMore) {
                            mView.showLoadMoreCompleted();
                        } else {
                            mView.showRefreshCompleted();
                        }
                    }
                });
        mSubscriptions.add(subscription);
    }

    @Override
    public List<OrderBill> getData() {
        return orderBillList;
    }

    @Override
    public void changeOrderState(String orderNumber) {
        unsubscribe();
        mView.showProgressDialog();
        final Subscription subscription = mDataRepositroy.updateOrderState(orderNumber, GlobalConstant.OrderState.UNPAID,
                GlobalConstant.OrderState.CANCELED)
                .map(new ServerResponseFunc<String>())
                .onErrorResumeNext(new HttpResponseFunc<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RxSubscriber<String>() {
                    @Override
                    public void onNext(String str) {
                        super.onNext(str);
                        mView.hideProgressDialog();
                        mView.reflashData();
                    }

                    @Override
                    protected void onError(ApiException ex) {
                        mView.hideProgressDialog();
                    }
                });
        mSubscriptions.add(subscription);
    }
}
